<?php
namespace frontend\widget;
use frontend\models\GoodsCategory;
use yii\base\Widget;
use yii\helpers\Url;
use yii\helpers\VarDumper;

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 17-4-11
 * Time: 22:05
 */
class NavWidget extends Widget
{
    public $expand = false;
    public $home =false;

    /**
     * @param $expand
     * @return string
     */
    public function run()
    {

        $expand = $this->expand?'on':'off';

        $home =$expand=='on'?'':'cat1';

        $show = $this->expand?'':'none';

        $cache = \Yii::$app->cache;
        $data = $cache->get('NavWiget'.$expand);
        if(!$data){

                $gcates = $this->getChildren(0);//获取所有的顶级分类
                $category1='';
                foreach ($gcates as $navbar){
                    //获取当前id的所有的子分类
                    $sonS = $this->getChildren($navbar['id']);
                    $category2='';
                    foreach ($sonS as $navbar2){
                        //获取子分类下的所有的子孙分类
                        $children = $this->getChildren($navbar2['id']);
                        $category3='';
                        foreach ($children as $navbar3){
                            $url3 = Url::to(['shop/list','goods_category_id'=>$navbar3['id']]);
                            $category3.= '<a href="'.$url3.'">'.$navbar3['name'].'</a>';
                        }

                        $url2 = Url::to(['shop/list','goods_category_id'=>$navbar2['id']]);
                        $category2.= '<dl class="dl_1st"><dt><a href="'.$url2.'"> '.$navbar2["name"].' </a></dt><dd> '.$category3.' </dd></dl> ';
                    }
                    //把子分类拼接
                    $url1 = Url::to(['shop/list','goods_category_id'=>$navbar['id']]);

                    $category1.='<div class="cat"><h3><a href="'.$url1.'">'.$navbar['name'].'</a><b></b></h3><div class="cat_detail none">'.$category2.'</div></div>';
                }

                $html ='<div><div class="nav w1210 bc mt10"><div class="category '.$home.' fl"><div class="cat_hd '.$expand;
                $html.='"><h2>全部商品分类</h2><em></em></div><div class="cat_bd '.$show.'">'.$category1.'</div></div>';

                //保存数据到缓存中
                $cache->set('NavWiget'.$expand,$html,'3600*24*7');

        }else{
//            echo <<<TEST
//<script>
//console.log('data is cache');
//</script>
//TEST;

            $html = $data;
        }



        return $html;


        /*foreach($gcates as $navbar){
            //循环一级导航条
        if($navbar['depth']==0){
                echo $i===0?'<div class="cat items">':'<div class="cat">';
                $i+=1;
                echo '<h3><a href="">'.$navbar['name'].'</a><b></b></h3><div class="cat_detail none">';
                //循环二级导航条
                foreach($gcates as $navbar2){
                    if($navbar2['parent_id']==$navbar['id']){
                        echo '<dl class="dl_1st"><dt><a href="">'.$navbar2["name"].'</a></dt><dd>';
                        //循环三级导航条
                   foreach ($gcates as $navbar3){
                       if($navbar3['parent_id']==$navbar2['id']){
                           echo '<a href="">'.$navbar3['name'].'</a>';
                       }
                   }
                echo '</dd></dl>';
                    }
                }
        echo '</div></div>';
        }
        }*/
        //返回数据
        //return $gcates;



    }


    public function getChildren($parent_id){
        //获取所有数据的数组
        if($parent_id ==0){
            $children = GoodsCategory::find()->where(['parent_id'=>$parent_id])->limit(13)->asArray()->all();
        }else{
            $children = GoodsCategory::find()->where(['parent_id'=>$parent_id])->asArray()->all();
        }

        return $children;
    }


}